package com.zx.dsptuneup_TriMic.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.jyn.vcview.VerificationCodeView;
import com.orhanobut.hawk.Hawk;
import com.zx.dsptuneup_TriMic.App;
import com.zx.dsptuneup_TriMic.Base.BaseActivity;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Tools.CVIPaperDialogUtils;
import com.zx.dsptuneup_TriMic.Tools.Constant;
import com.zx.dsptuneup_TriMic.Util.CommonAction;
import com.zx.dsptuneup_TriMic.Util.Constants;
import com.zx.dsptuneup_TriMic.Util.Netty.NettyListener;
import com.zx.dsptuneup_TriMic.Util.UserUtil;

import butterknife.BindView;

public class SettingActivity extends BaseActivity implements VerificationCodeView.OnCodeFinishListener {
    @BindView(R.id.ic_nav)
    ImageView Nav_left;
    @BindView(R.id.title)
    RelativeLayout navBar;
    @BindView(R.id.setting_rl_ll1)
    RelativeLayout rl11;
    @BindView(R.id.setting_rl_ll2)
    RelativeLayout rl22;
    @BindView(R.id.Switch_hotpot)
    RelativeLayout switch_hotpot;
    @BindView(R.id.setting_text_1)
    TextView setting_text_1;

    private Dialog joinMeetingDialog;
    private VerificationCodeView join_invite_codeview;
    private RelativeLayout tips;
    private int groupId = -1;
    private final String TAG = "SettingActivity.class";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);


        Intent intent = getIntent();
        groupId = intent.getIntExtra(Constant.GroupID,0);
        initUI();
    }

    @Override
    public void onTextChange(View view, String content) {
        if (view == join_invite_codeview && content.length()<4) {
            tips.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onComplete(View view, String content) {
        Log.d("codeEditView","//输入内容改变后的回调===="+content);
        if (content.length() <= 0) {
            Toast.makeText(SettingActivity.this,"输入不能为空",Toast.LENGTH_LONG);
            return;
        }else if(content.length() == 4){
            if(content.equals(UserUtil.DevelopCode)){
                Hawk.put(UserUtil.DevelopMode, true);
                UserUtil.isDevelopMode = true;

                InputMethodManager imm = (InputMethodManager) joinMeetingDialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(joinMeetingDialog.getWindow().getDecorView().getWindowToken(), 0); // 解决键盘无法关闭问题

                joinMeetingDialog.dismiss();
                Toast.makeText(SettingActivity.this,"开发者模式切换成功!",Toast.LENGTH_SHORT).show();
                refreshUI();
            }else {
                tips.setVisibility(View.VISIBLE);
                return;
            }
        }else{
            tips.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 开发者模式操作权限码
     */
    private void joinMeetingDialog() {
        joinMeetingDialog = new Dialog(SettingActivity.this, R.style.update_dialog);
        View view = LayoutInflater.from(SettingActivity.this).inflate(R.layout.diaog_verify_invite_code, null);//加载自己的布局
        join_invite_codeview = view.findViewById(R.id.verificationcodeview);
        join_invite_codeview.setOnCodeFinishListener(this);
        ImageView close_img = view.findViewById(R.id.ima_close);
        ImageView imageView = view.findViewById(R.id.clear_ima);
        tips = (RelativeLayout) view.findViewById(R.id.tipInfo);
        tips.setVisibility(View.INVISIBLE);
        joinMeetingDialog.setContentView(view);//这里还可以指定布局参数
        joinMeetingDialog.setCancelable(false);// 不可以用“返回键”取消
        joinMeetingDialog.show();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                join_invite_codeview.setEmpty();
            }
        });
        close_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinMeetingDialog.dismiss();
            }
        });
    }

    private void refreshUI() {
        if(UserUtil.isDevelopMode){
            setting_text_1.setText("开发者模式");
        }else {
            setting_text_1.setText("普通模式");
        }
    }

    protected void initUI() {
        ImmersionBar.with(this)
                .transparentBar()
                .statusBarColor(R.color.home_bg)
                .statusBarDarkFont(false)
                .init();

        refreshUI();

        rl11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(UserUtil.isDevelopMode){
                    CVIPaperDialogUtils.showCustomDialog(SettingActivity.this, "切回普通模式?", null, "确定", true, new CVIPaperDialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onClickButton(boolean clickConfirm, boolean clickCancel) {
                            if (clickConfirm){
                                Hawk.put(UserUtil.DevelopMode, false);
                                UserUtil.isDevelopMode = false;
                                Toast.makeText(SettingActivity.this,"普通模式切换成功!",Toast.LENGTH_SHORT).show();
                                refreshUI();
                            }
                        }
                    });


                }else {
                    joinMeetingDialog();
                }
            }
        });

        rl22.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, StrategyActivity.class);
                intent.putExtra(Constant.GroupID, groupId);
                startActivity(intent);
            }
        });


        // 切换热点
        switch_hotpot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setMessage("确定要切换热点吗");
                builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        App.getContext().getNettyClient().disconnect();
                        CommonAction.getInstance().OutSign();
                        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
        // 返回
        Nav_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



}
